// pages/my/vipcenter/vipcenter.js
import { axios } from '../../../utils/ajax.js'
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //购买弹窗
    showbuyopener: false,
    maskstate: false,
    data:'',
    userid:'',
    token:'',
    //用户选择项
    userchecked:'',
    //支付弹窗信息
    payinfo:'',
    //规则富文本
    rulenode:'',
    username:'',
    iPhoneX:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let username = wx.getStorageSync('username')
    console.log(username)
    this.setData({
      username: username
    })


    //x适配
    console.log(app.globalData.model)
    if (app.globalData.model.substr(0, 8) == 'iPhone X') {
      this.setData({
        iPhoneX: true
      })
    }

    let { userid = '', token = '' } = wx.getStorageSync('userinfo') 
    console.log(userid, token)
    this.setData({
      userid: userid,
      token: token
    })
    axios({
      url:'/VipUser/memberCenter',
      data: {
        userid: userid,
        token: token
      }
    }).then((res)=>{
      console.log(res)
      this.setData({
        data: res.data,
        rulenode: res.data.stipulation
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //购买会员
  buyvip () {
    let userchecked = this.data.userchecked
    console.log(userchecked)
    if (userchecked){
      axios({
        url: "/VipUser/memberPay",
        data: {
          userid: this.data.userid,
          token: this.data.token,
          member_type: userchecked.membertype,
          year_type: userchecked.yeartype
        },
      }).then((res) => {
        console.log(res)
        this.setData({
          showbuyopener: true,
          maskstate: true,
          payinfo: res.data.memberInfo
        })
      })
    }else{
      wx.showToast({
        title: '请选择购买内容',
        icon:"none"
      })
    }
  },
  //关闭购买弹窗
  closebuyopener (){
    this.setData({
      showbuyopener: false,
      maskstate: false
    })
  },
  //付钱
  pay () {
    let userchecked = this.data.userchecked
    //生成订单
    axios({
      url: '/VipUser/createUserOrder',
      data: {
        userid: this.data.userid,
        token: this.data.token,
        member_type: userchecked.membertype,
        year_type: userchecked.yeartype
      }
    }).then((res)=>{
      axios({
        url: '/Pay/memberWxPay',
        data: {
          user_id: this.data.userid,
          token: this.data.token,
          order_no: res.data.orderNo
        }
      }).then((res) => {
        console.log(res)
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success:  (res)=>{
            axios({
              url: '/VipUser/memberCenter',
              data: {
                userid: this.data.userid,
                token: this.data.token
              }
            }).then((res) => {
              console.log(res)
              this.setData({
                data: res.data,
                rulenode: res.data.stipulation,
                showbuyopener: false,
                maskstate: false
              })
            })
          },
          fail: (res) =>{
            console.log(res)
          }
        })
      })
    })
  },
  //查看协议
  lookprotocol (e){
    let url = e.currentTarget.dataset.link
    wx.navigateTo({
      url: `../../webview/webview?linkId=${url}`,
    })
  },
  //选择商品规格
  choosethis (e) {
    let checkbox = this.data.checkbox
    console.log(e.currentTarget.dataset)
    this.setData({
      userchecked: e.currentTarget.dataset
    })
  }
})